﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
//using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using PowerCreator.LMS.Web.Grade;
using PowerCreator.LMS.Grade.Entity;
using PowerCreator.LMS.Common.Script;
using System.Collections.Generic;

using PowerCreator.LMS.Web.Project;
using PowerCreator.LMS.Project.Entity;
using PowerCreator.LMS.Course.Entity;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Web.Paper;
using PowerCreator.LMS.Paper.Entiry;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;

public partial class sysadmin_GradeManager_ManagerSkill : PowerCreator.LMS.Web.Base.BasePage
{
    GradeManager objGradeManager = new GradeManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BinderRPTSkillType();

            if (Request.QueryString["SkillTypeID"] != null)
            {
                ViewState["SkillTypeID"] = Request.QueryString["SkillTypeID"].ToString();
            }
            InitPage();
            string search = GetSearch();
            BinderPG(search);

        }
    }

    protected override void InitPage()
    {
        lbl_InfoTitle_ManagerDownload.Text = GetLanageValueByKey("lab_AddSkill_SkillManager", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_ManagerDownload.Text = "新下载资源列表";
        btnAddNew.Text = GetLanageValueByKey("btn_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);

        chkAll.Text = GetLanageValueByKey("chkbox_ManagerQuestion_chkboxAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkCancel.Text = GetLanageValueByKey("chkbox_ManagerQuestion_chkboxCancel", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_DelCheck.Text = GetLanageValueByKey("btnDel", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnAddNew.Text = GetLanageValueByKey("btn_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_DelCheck.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
        //btn_IsCheck.Text = GetLanageValueByKey("btnAuditing", PowerCreator.LMS.Language.LanagetItemType.Page);

        base.InitPage();

        //btnAddNew.Visible = false;
        //if (IsLicensed(PowerCreator.LMS.Action.Core.ActionInfo.AddJob))
        //    btnAddNew.Visible = true;

    }

    private void BinderRPTSkillType()
    {
        string orderList = "SkillTypeID asc";
        int iCount=objGradeManager.CountSkillType("");
        DataTable dt = objGradeManager.QuerySkillType("", orderList, iCount, 1);
        //DataTable dt = opProjectResource.QueryProjectResource(search);
        //List<VOTSkillType> list = FillList(dt);
        rpt_SkillType.DataSource = dt;
        rpt_SkillType.DataBind();
    }
    private string GetSearch()
    {
        string search = "  1=1 ";
        if (!string.IsNullOrEmpty(txt_KeyWord.Text))
            search =search+ string.Format(" and SkillName like '%{0}%'", txt_KeyWord.Text.Trim());
        if (ViewState["SkillTypeID"] != null)
        {
            if(ViewState["SkillTypeID"].ToString()!="0")
                search += " and FKSkillTypeId=" + ViewState["SkillTypeID"].ToString();
        }
        return search;
    }


    private void BinderPG(string search)
    {
        int iCount = objGradeManager.CountSkill(search);
        DataTable dt = objGradeManager.QuerySkill(search, "", iCount, 1);
        rptDownload.DataSource = dt;
        rptDownload.DataBind();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPG(search);
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        string url = "AddSkill.aspx";
        Script.RedirectURL(url);
    }

    protected void rptDownload_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Label lbl_msg = (Label)e.Item.FindControl("lbl_msg");

        if (e.CommandName.ToLower() == "leditor")
        {
            //if (IsLicensed("DownloadUpdate"))
            //{
            string url = "AddSkill.aspx?SkillID=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);
            //}
            //else
            //{
            //    lbl_msg.Text = GetLanageValueByKey("msg_ManagerNew_checkmsg", PowerCreator.LMS.Language.LanagetItemType.Page);
            //}
        }
        else if (e.CommandName.ToLower() == "ldel")
        {
            //if (IsLicensed("DownloadDel"))
            //{
            int SkillID = int.Parse(e.CommandArgument.ToString());
            VOTSkill vo = new VOTSkill();
            vo = objGradeManager.LoadSkill(SkillID);
            if (objGradeManager.RemoveSkill(SkillID))
            {
                VOTSkillType objVOTSkillType = objGradeManager.LoadSkillType(vo.FKSkillTypeId); ;
                objVOTSkillType.SkillTypeScore -= vo.SkillScore;
                objGradeManager.UpdateSkillType(objVOTSkillType);
                string search = GetSearch();
                BinderPG(search);
            }
            else
            {
                lbl_msg.Text = GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page);
            }
            //}
            //else
            //{
            //    lbl_msg.Text = GetLanageValueByKey("msg_ManagerNew_checkmsg", PowerCreator.LMS.Language.LanagetItemType.Page);
            //}
        }


    }
    protected void rptDownload_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            LinkButton lbtn = (LinkButton)e.Item.FindControl("lnkbtn_del");
            lbtn.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
        }
    }
    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        chkCancel.Checked = false;
        //全选 / 取消
        for (int i = 0; i < rptDownload.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptDownload.Items[i].FindControl("Chbox_Check");
            if (chkAll.Checked)
                cbox.Checked = true;
            else
                cbox.Checked = false;
        }
    }
    protected void chkCancel_CheckedChanged(object sender, EventArgs e)
    {
        chkAll.Checked = false;
        //反选
        for (int i = 0; i < rptDownload.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptDownload.Items[i].FindControl("Chbox_Check");
            if (cbox.Checked)
                cbox.Checked = false;
            else
                cbox.Checked = true;
        }
    }
    protected void btn_DelCheck_Click(object sender, EventArgs e)
    {
        try
        {
            for (int i = 0; i < rptDownload.Items.Count; i++)
            {
                CheckBox cbox = (CheckBox)rptDownload.Items[i].FindControl("Chbox_Check");
                Label lab_DownID = (Label)rptDownload.Items[i].FindControl("lab_DownID");
                if (cbox.Checked)
                {
                    //if (IsLicensed("DownloadDel"))
                    //{
                    int SkillID = int.Parse(lab_DownID.Text);
                    VOTSkill vo = new VOTSkill();
                    vo = objGradeManager.LoadSkill(SkillID);
                    if (objGradeManager.RemoveSkill(SkillID))
                    {
                        VOTSkillType objVOTSkillType = objGradeManager.LoadSkillType(vo.FKSkillTypeId); ;
                        objVOTSkillType.SkillTypeScore -= vo.SkillScore;
                        objGradeManager.UpdateSkillType(objVOTSkillType);
                    }
                    else
                    {
                        Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
                    }
                    //}
                    //else
                    //{
                    //    Script.Alert(this, "msg", GetLanageValueByKey("msg_ManagerNew_checkmsg", PowerCreator.LMS.Language.LanagetItemType.Page));
                    //}
                }
            }
            string search = GetSearch();
            BinderPG(search);
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkOK", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
        catch
        {
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
    }


    protected void rpt_SkillType_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "lshowskill")
        {

            string url = "ManagerSkill.aspx?SkillTypeID=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);

            //Label lbl_Msg = (Label)e.Item.FindControl("lbl_projectresource_msg");
            //Label lbl_ProresResIsQueue = (Label)e.Item.FindControl("lbl_ProresResIsQueue");       //资源有序学习
            //Label lbl_ProjectResourcePassState = (Label)e.Item.FindControl("lbl_ProjectResourcePassState");         //资源当前状态

            //if (lbl_ProresResIsQueue.Text == "1")
            //{
            //    int index = e.Item.ItemIndex;
            //    if (index != 0)
            //    {
            //        index = index - 1;
            //        Label lbl_ProresResIsQueueRPT = (Label)rpt_ProjectResource.Items[index].FindControl("lbl_ProresResIsQueue");       //资源有序学习
            //        Label lbl_ProjectResourcePassStateRPT = (Label)rpt_ProjectResource.Items[index].FindControl("lbl_ProjectResourcePassState");         //资源当前状态
            //        if (lbl_ProjectResourcePassStateRPT.Text != "1")//通过
            //        {
            //            //lbl_msg_description.Text = "该方案资源必须学完上一级课程!";
            //            lbl_msg_description.Text = GetLanageValueByKey("web_ProjectResource_msg_m3", PowerCreator.LMS.Language.LanagetItemType.Page);
            //            Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
            //            return;
            //        }
            //    }
            //}

            //VOTProjectResource voProjectResource = opProjectResource.LoadProjectResource(int.Parse(e.CommandArgument.ToString()));
            //ViewState["Search:ProjectResourceID"] = voProjectResource.ProresID.ToString();
            //if (voProjectResource.ProresClass == Convert.ToInt16(CoursersClass.Courseware))
            //{
            //    string search = GetSearch();
            //    BinderRPT(search);
            //}
            //else if (voProjectResource.ProresClass == Convert.ToInt16(CoursersClass.Paper))
            //{
            //    //自己创建对象绑定数据
            //    List<CourseResourceInfo> list = new List<CourseResourceInfo>();
            //    CourseResourceInfo item = new CourseResourceInfo();
            //    item.CourseID = voProjectResource.FKProresCourseID;
            //    //item.CourseName = "无课程";
            //    item.CoursersRestpName = GetLanageValueByKey("web_ProjectResource_gv_c5", PowerCreator.LMS.Language.LanagetItemType.Page);
            //    item.CourseresID = 0;
            //    item.CoursersClassID = 2;
            //    item.CoursersClassName = opCourse.GetCoursersClassByData(item.CoursersClassID, GetLanageValueByKey("Resource_Class1", PowerCreator.LMS.Language.LanagetItemType.Page), GetLanageValueByKey("Resource_Class2", PowerCreator.LMS.Language.LanagetItemType.Page));
            //    item.CoursersResourceID = 0;
            //    item.CoursersRestpID = 0;
            //    //item.CoursersRestpName = "无分类";
            //    item.CoursersRestpName = GetLanageValueByKey("web_ProjectResource_gv_c6", PowerCreator.LMS.Language.LanagetItemType.Page);
            //    VOPaper voPaper = opPaper.LoadPaper(voProjectResource.FKProresPaperID);
            //    item.ResourceName = voPaper.PaperName;
            //    item.ProjectResourceID = voProjectResource.ProresID;
            //    //获取该资源当前分数
            //    string search1 = string.Format("FKSpcoelogTrainproID={0} and FKSpcoelogTrainclaID={1} and FKSpcoelogUserID={2} and FKSpcoelogProresID={3}", ViewState["ProjectID"], ViewState["ProjectClassID"], GetLoginEntity.UserID, voProjectResource.ProresID);
            //    DataTable dt = opProjectPlan.QueryProjectResource(search1, 1, 1);
            //    if (dt.Rows.Count == 1)
            //    {
            //        VOTStudentPassCouOrExLog voProjectResourcePlan = opProjectPlan.LoadProjectResourcePlan(int.Parse(dt.Rows[0]["SpcoelogID"].ToString()));
            //        item.ProjectResourcePlanState = voProjectResourcePlan.SpcoelogState;
            //        item.StudentPlanValue = _descriptionPaper + voProjectResourcePlan.SpcoelogPassScore.ToString();

            //    }
            //    item.ProjectResourcePlanStateName = opProjectPlan.GetProjectPlanByState(item.ProjectResourcePlanState, ProjectPlan.ProjectResourcePlan);

            //    list.Add(item);
            //    rpt_Resource.DataSource = list;
            //    rpt_Resource.DataBind();

            //    NewProjectPlanInfo info = new NewProjectPlanInfo();
            //    info.ProjectID = int.Parse(ViewState["ProjectID"].ToString());
            //    info.ClassID = int.Parse(ViewState["ProjectClassID"].ToString());
            //    info.UserID = GetLoginEntity.UserID;

            //    for (int i = 0; i < rpt_Resource.Items.Count; i++)
            //    {
            //        LinkButton lnkbtn_show = (LinkButton)rpt_Resource.Items[i].FindControl("lnkbtn_show");
            //        int exampapercount = opProjectPlan.QueryExamPaperCount(voProjectResource, info, 0);   //己考试次数
            //        if (exampapercount != 0)
            //        {
            //            //lnkbtn_show.Text = "立即补考";
            //            lnkbtn_show.Text = GetLanageValueByKey("web_ProjectResource_gv_c2", PowerCreator.LMS.Language.LanagetItemType.Page);
            //        }
            //    }
            //}
        }
    }

}
